//
//  WAEnsureTransactionController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/21.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAEnsureTransactionController.h"
#import "WAGoodModel.h"
#import "WAAddressModel.h"
#import "WAAddressController.h"

@interface WAEnsureTransactionController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *addressContainViewCOnsHeight;
@property (weak, nonatomic) IBOutlet UIView *addressDetailVIew;
@property (weak, nonatomic) IBOutlet UILabel *nameLB;
@property (weak, nonatomic) IBOutlet UILabel *phoneLB;
@property (weak, nonatomic) IBOutlet UILabel *detailAddressLB;


@property (weak, nonatomic) IBOutlet UILabel *priceLB;
@property (weak, nonatomic) IBOutlet UILabel *goodsNameLB;
@property (weak, nonatomic) IBOutlet UILabel *goodPriceLB;
@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;
@property (weak, nonatomic) IBOutlet UILabel *goodNumLB;

@property (strong, nonatomic)NSString *goodID;
@property (strong, nonatomic)WAGoodModel *goodModel;
@property (strong, nonatomic)WAAddressModel *addressModel;

@property (weak, nonatomic) IBOutlet UILabel *totalPriceLB;
@end

@implementation WAEnsureTransactionController

- (instancetype)initWithID:(NSString *)goodID
{
    self = [super init];
    if (self) {
        self.goodID = goodID;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [WNTool showLoading];
    [self getDefaultAddress];
    [self getGoodDetail];
}

//没有默认地址再获取地址列表
- (void)getAddressList{
    [WNBusinessRequest getAddressListWithPageNum:1 Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSArray *memberList = [WAAddressModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (memberList.count == 0) {
        }else{
            self.addressModel = memberList.firstObject;
        }
        [self upddateViews];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

//先获取默认地址
- (void)getDefaultAddress{
    [WNBusinessRequest getDefauleAddressListSucc:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSArray *memberList = [WAAddressModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (memberList.count == 0) {
            [self getAddressList];
        }else{
            self.addressModel = memberList.firstObject;
            [self upddateViews];
        }
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        
    }];
}

- (void)getGoodDetail{
    [WNTool showLoading];
    [WNBusinessRequest getSinglGoos:self.goodID Succ:^(id  _Nonnull responseObject) {
        [WNTool hideLoading];
        NSDictionary *dict = (NSDictionary *)responseObject;
        self.goodModel = [WAGoodModel mj_objectWithKeyValues:dict];
        [self upddateViews];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}

- (void)upddateViews{
    [WNTool hideLoading];
    if (self.addressModel) {
        self.addressDetailVIew.hidden = NO;
        self.nameLB.text = self.addressModel.name;
        self.phoneLB.text = [self.addressModel.phone phone];
        self.detailAddressLB.text = self.addressModel.address;
        self.addressContainViewCOnsHeight.constant = 80;
    }else{
        self.addressDetailVIew.hidden = YES;
        self.addressContainViewCOnsHeight.constant = 44;
    }
    self.goodNumLB.text = [NSString stringWithFormat:@"商品编号：%zd",self.goodModel.ID];
    self.goodPriceLB.text = [NSString stringWithFormat:@"价格：%.1f雨露",self.goodModel.yuluprice];
    self.goodsNameLB.text = self.goodModel.name;
    [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:self.goodModel.image0]];
    self.totalPriceLB.text = [NSString stringWithFormat:@"合计：%.1f雨露",self.goodModel.yuluprice];
}

- (IBAction)addAddressAction:(id)sender {
    WAAddressController *VC = [WAAddressController new];
    VC.seletedID = self.addressModel.ID;
    [self.navigationController pushViewController:VC animated:YES];
    @weakify(self)
    [VC setSelectBlcok:^(WAAddressModel * _Nonnull model) {
        @strongify(self)
        if (model) {
            self.addressModel = model;
        }else{
            self.addressModel = nil;
            [self getDefaultAddress];
        }
        [self upddateViews];
    }];
}


- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)buyAction:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否购买此商品" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (!self.addressModel) {
            [WNTool show:@"请输入收货地址！"];
            return;
        }
        [WNTool showLoading];
        [WNBusinessRequest addOrderWithGoodsID:self.goodModel.ID addressid:self.addressModel.ID price:self.goodModel.yuluprice number:1 Succ:^(id  _Nonnull responseObject) {
            [WNTool show:@"下单成功,请支付"];
            NSDictionary *dict = (NSDictionary *)responseObject;
            [self payActionWithOrderID:[dict[@"id"] integerValue]];
        } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
            [WNTool show:mes];
        }];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)payActionWithOrderID:(NSInteger)orderID{
   UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UITextField *TF = alert.textFields.firstObject;
        [self verificationPayPassword:TF.text orderID:orderID];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [self backAction:nil];
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"请输入支付密码";
        textField.secureTextEntry = YES;
    }];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)verificationPayPassword:(NSString *)pwd orderID:(NSInteger)orderID{
    [WNTool showLoading];
    [WNBusinessRequest payWithOrderID:orderID payPwd:pwd Succ:^(id  _Nonnull responseObject) {
        [WNTool show:@"支付成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self backAction:nil];
        });
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
    
}

@end
